package com.itheima.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.bean.Orders;
import com.itheima.common.PageParam;
import com.itheima.common.R;
import com.itheima.dto.OrdersDto;
import com.itheima.service.OrderService;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 提交订单
     * @param order
     * @return
     */
    @PostMapping("/order/submit")
    public R submitOrder(@RequestBody Orders order){

        int row = orderService.add(order);

        if(row > 0 ){

            shoppingCartService.clean();

            return R.success("提交订单成功！");
        }
        return R.success("提交订单失败！");
    }

    /**
     * 订单分页查询
     * @param pageParam
     * @return
     */
    @GetMapping("/order/page")
    public R page(PageParam pageParam){
        IPage<Orders> page = orderService.findpage(pageParam);
       return R.success(page);
    }

    /**
     * 最新订单查看
     *
     * @param pageParam
     * @return
     */
    @GetMapping("/order/userPage")
    public R newestOrder(PageParam pageParam) {

        IPage<OrdersDto> newestOrder = orderService.newestOrder(pageParam);

        return R.success(newestOrder);
    }

    @PutMapping("/order")
    public R paisong(@RequestBody Orders order){
        int row = orderService.paisong(order);
        if(row > 0) return R.success("");
        return R.error("");
    }
}
